﻿using System;
using System.Web;
using System.Web.Mvc;
using System.Text.RegularExpressions;
using System.IO;
using System.Drawing.Imaging;
using System.Drawing;
using Wild2.Models.Captcha;
using System.Collections.Generic;

namespace Wild2.Controllers
{
    public class LoginController : Controller
    {
        //
        // GET: /Login/
        
        public ActionResult Index()
        {
            Models.Pages.Login.Index page = new Models.Pages.Login.Index();
            return View(page);
        }

        [HttpPost]
        public ActionResult Index(String loginbox, String passwordbox)
        {
            Models.Pages.Login.Index page = new Models.Pages.Login.Index();
            bool err = false;
            if (loginbox.Length < 4) { page.isloginShort = err = true; }
            if (loginbox.Length > 20) { page.isloginLong = err = true; }
            if (passwordbox.Length < 6) { page.isPasswordShort = err = true; }
            if (passwordbox.Length > 50) { page.isPasswordLong = err = true; }
            if (!err)
            {
                Int32? uid = Models.Auth.AuthOnSite.AuthPassword(loginbox, passwordbox);
                if (uid.HasValue)
                {
                    //Session[Wild2.Models.Constants.SessionConstants.USERID] = uid.Value;
                }
                else
                {
                    page.isUserNotFound = true;
                }
            }
            if (page.isUserNotFound == false) return RedirectToAction("Index", "Home");
            return View(page);
        }

        public ActionResult Register()
        {
            Models.Pages.Login.Register page = new Models.Pages.Login.Register();
            return View(page);
        }

        [HttpPost]
        public ActionResult Register(String loginbox, String passwordbox, String codebox)
        {
            Models.Pages.Login.Register page = new Models.Pages.Login.Register();
            bool err = false;
            if (loginbox.Length < 4) { page.isLoginShort = err = true; }
            if (loginbox.Length > 20) { page.isLoginLong = err = true; }
            if (passwordbox.Length < 6) { page.isPasswordShort = err = true; }
            if (passwordbox.Length > 50) { page.isPasswordLong = err = true; }
            if (Wild2.Models.Auth.RegisterOnSite._regEn.IsMatch(loginbox) && Wild2.Models.Auth.RegisterOnSite._regRu.IsMatch(loginbox)) { page.isDifferentLanguages = err = true; }
            if (Wild2.Models.Auth.RegisterOnSite._reg.IsMatch(loginbox)) { page.isBadCharacters = err = true; }
            if (!Models.Auth.RegisterOnSite.CheckCode(codebox)) { page.isBadCode = err = true; }
            if (!err)
            {
                Int32? userId = Models.Auth.RegisterOnSite.Register(loginbox, passwordbox);
                if (userId.HasValue)
                {
                    Models.Auth.RegisterOnSite.UpdateCode(codebox, userId.Value);
                    page.isSuccess = true;
                }
                else
                {
                    page.isUserExist = true;
                }
            }
            if (page.isSuccess) return RedirectToAction("Index", "Home");
            return View(page);
        }

        public ActionResult Logout()
        {
            Session.Remove("UserID");
            Session.Remove("StartPeriodData");
            Session.Remove("UserNick");
            Session.Remove("ActiveGirlID");
            if (Request.Cookies["PrivateKey"] != null)
            {
                HttpCookie cookie = new HttpCookie("PrivateKey");
                cookie.Value = "";
                cookie.Expires = DateTime.Now.AddDays(-1D);
                Response.Cookies.Add(cookie);
            }
            return RedirectToAction("Index", "Home");
        }

        public FileResult GetCaptcha(String postfix)
        {
            CaptchaImage ci = new CaptchaImage();
            Bitmap bmTemp = ci.RenderImage();
            MemoryStream memStream = new MemoryStream();
            bmTemp.Save(memStream, ImageFormat.Png);
            memStream.WriteTo(Response.OutputStream);

            Captcha ct = new Captcha(ci.Text, ci.RenderedAt);
            Session.Add(Wild2.Models.Constants.SessionConstants.CAPCHA + postfix, ct);
            bmTemp.Dispose();

            return File(memStream, "image/png");
        }
    }
}
